package com.base.system.managedBean;

import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.common.config.CommonKeyConst;
import com.base.commonKey.entity.CommonKeyType;
import com.base.commonKey.service.ICommonKeyTypeDao;
import com.base.commons.SpringBeanHolder;

@ManagedBean(name = "system_commonKeyTypeListBean", scope = ManagedBeanScope.REQUEST)
public class CommonKeyTypeListBean {

	@Inject
	private FacesContext context;

	@Bind(id = "cnkGrid")
	private UIDataGrid cnkGrid;

	// @DataModel(id = "cnkGrid")
	// private List<CommonKeyType> commonKeyTypes = new
	// ArrayList<CommonKeyType>();

	ICommonKeyTypeDao dao = SpringBeanHolder.getBean("commonKeyTypeDao");

	@SaveState
	@Bind(id = "codeSearch", attribute = "value")
	private String codeSearch_value;

	@SaveState
	@Bind(id = "nameSearch", attribute = "value")
	private String nameSearch_value;
	@SaveState
	@Bind(id = "modulesSearch", attribute = "value")
	private String modulesSearch;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			codeSearch_value = null;
			nameSearch_value = null;
			modulesSearch = null;
		}
	}

	@DataModel(id = "cnkGrid")
	private List<CommonKeyType> getCnkGrid() {
		String hql = "select type from CommonKeyType type where 1=1";
		if (codeSearch_value != null && !"".equals(codeSearch_value)) {
			hql = hql + " and type.typeCode like '%" + codeSearch_value + "%'";
		}
		if (nameSearch_value != null && !"".equals(nameSearch_value)) {
			hql = hql + " and type.typeName like '%" + nameSearch_value + "%'";

		}
		if (modulesSearch != null && !"".equals(modulesSearch)) {
			hql = hql + " and type.module like '%" + modulesSearch + "%'";
		}
		hql = hql + " order by type.module";
		// if (codeSearch_value != null && !"".equals(codeSearch_value)) {
		// return dao.findAllByNamedQuery("commonKeyType.searchByCode", "%"
		// + codeSearch_value + "%");
		// } else if (nameSearch_value != null && !"".equals(nameSearch_value))
		// {
		// return dao.findAllByNamedQuery("commonKeyType.searchByName", "%"
		// + nameSearch_value + "%");
		// } else //if (codeSearch_value != null && !"".equals(codeSearch_value)
		// && nameSearch_value != null && !"".equals(nameSearch_value)) {
		// return dao.findAllByNamedQuery("commonKeyType.searchByCodeAndName",
		// "%" + codeSearch_value + "%", "%" + nameSearch_value + "%");
		// }
		// /return dao.findAllByNamedQuery("commonKeyType.getAllType");
		return dao.findByHql(hql);
	}

	@Action(id = "create")
	public void create() {
		String url = "/modules/system/addCommonKeyType.faces";
		url = context.getExternalContext().getRequestContextPath() + url;
		String script = "addCenterLayoutTab('" + CommonKeyConst.ADD_TAB + "','"
				+ "新建配置项" + "', '" + url + "')";
		Browser.execClientScript("window.top", script);
	}

	@Action(id = "edit")
	public void edit() {
		if (initSelected()) {
			String url = "/modules/system/editCommonKeyType.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('" + CommonKeyConst.EDIT_TAB
					+ "','" + "编辑配置项" + "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "delete")
	public void delete() {
		Object[] objs = cnkGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			return;
		}
		dao.delete((CommonKeyType) objs[0]);
		// data.remove((CommonKey) objs[0]);
		cnkGrid.reload();
	}

	private boolean initSelected() {
		Object[] objs = cnkGrid.getSelectedValues();
		// System.out.println("取得的选取值是：" + objs);
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return false;
		}
		context.getExternalContext().getSessionMap().put(
				CommonKeyConst.COMMONKEYTYPE_SELECT_ROW_DATA,
				(CommonKeyType) objs[0]);
		return true;
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	public void showCommonKeyType() {
		// /if (initSelected()) {
		Object[] objs = cnkGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
		} else {

			CommonKeyType cktype = (CommonKeyType) objs[0];
			String url = "/modules/system/showCommonKeyTree.faces?keyType="
					+ cktype.getTypeCode();
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ CommonKeyConst.COMMONKEYTYPE_SHOW_TAB + "','" + "查看配置项"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "query")
	public void query() {
		cnkGrid.reload();
	}

	@Action(id = "reset")
	public void reset() {
		codeSearch_value = null;
		nameSearch_value = null;
		modulesSearch = null;
		cnkGrid.reload();
	}
}
